package com.egreen.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.admin.common.AdminException;
import com.egreen.common.States;
import com.egreen.common.pojo.entity.admin.Admin;
import com.egreen.admin.dao.AdminDao;
import com.egreen.admin.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.pojo.DTO.user.loginaccount.LoginByPasswordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements IAdminService {
    @Autowired
    AdminDao adminDao;

    @Override
    /**
     * 管理员登录（使用密码登录）
     */
    public Admin Adminlogin(LoginByPasswordDTO loginByPasswordDTO) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number", loginByPasswordDTO.getLoginKey());
        Admin admin = adminDao.selectOne(queryWrapper);
        if (admin == null) {
            throw new AdminException(States.ERR_ADMIN_NOT_FOUND);
        } else {
            if (admin.getPassword().equals(loginByPasswordDTO.getPassword())) {
                return admin;
            } else {
                throw new AdminException(States.ERR_JWT_MALFORMED, "密码错误");
            }
        }
    }
}
